<?php
class ErrorController extends Zend_Controller_Action
{
    public function init()
    {
       //$this->_helper->viewRenderer->setNoRender();
       $this->_helper->layout->disableLayout();
    }

    public function errorAction()
    {
         // Ensure the default view suffix is used so we always return good
        // content
        $this->_helper->viewRenderer->setViewSuffix('phtml');

        $config = CMS_Config::getInstance(CMS_INSTANCE_SERVER);

        // Grab the error object from the request
        $errors = $this->_getParam('error_handler');

        switch ($errors->type) {
          case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
          case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            // 404 error -- controller or action not found
            $this->getResponse()->setHttpResponseCode(404);
            $this->view->message = 'Page not found';
            $this->view->code  = 404;
            if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER) {
              $this->view->info = sprintf(
                          'Unable to find controller "%s" in module "%s"',
                          $errors->request->getControllerName(),
                          $errors->request->getModuleName()
                        );
            }
            if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION) {
              $this->view->info = sprintf(
                          'Unable to find action "%s" in controller "%s" in module "%s"',
                          $errors->request->getActionName(),
                          $errors->request->getControllerName(),
                          $errors->request->getModuleName()
                        );
            }
            break;
          case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
          default:
            // application error
            $this->getResponse()->setHttpResponseCode(500);
            $this->view->message = 'Application error';
            $this->view->code  = 500;
            $this->view->info  = $errors->exception;
            //Email Debug!!
            if($config->debug->email->active)
                $this->sendMailError($errors->exception->getMessage(), $errors->exception->getTraceAsString());

            break;
        }
        $this->view->title = 'Error!';
        $this->view->heading = 'Error!';

        // pass the environment to the view script so we can conditionally
        // display more/less information

        $this->view->mode   = $config->debug->mode;

        // pass the actual exception object to the view
        $this->view->exception = $errors->exception;

        // pass the request to the view
        $this->view->request = $errors->request;



    }

     private function sendMailError($message, $trace)
    {
        $subject = "Error - ".CMS_INSTANCE_SERVER;

        $config = CMS_Config::getInstance(CMS_INSTANCE_SERVER);

        $body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                 <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                        <title>ERROR</title>
                    </head>
                    <body>
                        <p>SERVER: {server}</p>
                        <hr/>
                        <p>MESSAGE: {message}</p>
                        <p>DATE: {date}</p>
                        <p>TRACE: {trace}</p>
                    </body>
                  </html>';

        $body = str_replace("{server}", CMS_INSTANCE_SERVER, $body);
        $body = str_replace("{message}", $message, $body);
        $body = str_replace("{date}", date("Y-m-d H:m:i"), $body);
        $body = str_replace("{trace}", $trace, $body);


        $mail = new Common_System_Mail();
        $mail->addTo($config->debug->email->address);
        $mail->setBodyHtml($body);
        $mail->setFrom($config->email->from, $config->email->fromname);
        $mail->setSubject($subject);
        $mail->sendUsingSMTP($config->email->smtp->host,$config->email->smtp->username,$config->email->smtp->password);

    }
}
?>
